package com.etc.web;



import com.etc.model.Admin;
import com.etc.model.Student;
import com.etc.model.Teacher;
import com.etc.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/3/21.
 */
@Controller
@RequestMapping("/administration")
public class UserController {


    @Autowired
    private UserServiceImpl userService;
    @RequestMapping("/form")
    /* @ResponseBody*/
    public ModelAndView getform() {
        ModelAndView mv = new ModelAndView();

        mv.setViewName("/user-form.btl");

        return
                mv;
    }
    //管理员注册
    @RequestMapping(value = "/register" , method = RequestMethod.POST,consumes = "application/json")
    @ResponseBody
    public String RegisterAdmin(@RequestBody Admin admin) {
        userService.registerAdmin(admin);
        return "ok";
    }
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    /* @ResponseBody*/
    public ModelAndView adminLogin() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/adminlogin.btl");
        return
                mv;
    }
    @RequestMapping(value = "/login" , method = RequestMethod.POST,consumes = "application/json")
    @ResponseBody
    public Map<String,String> checkAdminExit(@RequestBody Admin admin, HttpSession httpSession) {
        Map<String,String> map=new HashMap<String, String>();
        httpSession.setAttribute("username", admin.getUsername());
        map=userService.checkAdminExit(admin);
        return  map;
    }
    @RequestMapping("/users")
    /* @ResponseBody*/
    public ModelAndView getUser(HttpSession httpSession) {
        ModelAndView mv = new ModelAndView();
        List<Teacher> teachers = userService.getTeacherInfo();
        List<Student> students = userService.getStudentInfo();
        mv.addObject("username",httpSession.getAttribute("username"));
        mv.addObject("teachers",teachers);
        mv.addObject("students",students);
        mv.setViewName("/users.btl");

        return
                mv;
    }
    //注册账号重复检测
    @RequestMapping(value = "/checkAdminUsername" , method = RequestMethod.POST,consumes = "application/json")
    @ResponseBody
    public Map<String, String> loginRegisterExam(@RequestBody Admin data){
        Map<String,String> map=new HashMap<String, String>();
        map=userService.loginRegisterExam(data);
        return map;
    }
    @RequestMapping("/saveTeacher")
    /* @ResponseBody*/
    public String saveUser(Teacher teacher) {

        userService.saveTeacher(teacher);
        return "ok";



    }
    @RequestMapping("/saveStudent")
    public String saveStudent(Student student){
        userService.saveStudent(student);
        return "ok";
    }
    @RequestMapping("/insertStudent")
    public String insertStudent(Student student){
        userService.insertStudent(student);
        return "ok";
    }
    @RequestMapping("/insertTeacher")
    /* @ResponseBody*/
    public String insertUser(Teacher teacher) {


        userService.insertTeacher(teacher);
        return "ok";

    }
    @RequestMapping("/deleteStudent")
    public String deleteStudent(int id){
        userService.deleteStudent(id);
        return "delete";
    }
    @RequestMapping("/deleteTeacher")
    public String deleteTeacher(int id){
        userService.deleteTeacher(id);
        return "delete";
    }
   /* @RequestMapping("/api/insertTeacher")
    @ResponseBody
    public String insertapiteacher(@RequestBody Teacher teacher) {
        databaseTeacherInfo.save(teacher);

        return "ok";

    }*/
    /*@RequestMapping("/api/insertStudent")
    @ResponseBody
    public String insertapistudent(@RequestBody Student student) {

        databaseStudentInfo.save(student);

        return "ok";

    }*/
   /* @RequestMapping("/api/deleteTeacher")
    @ResponseBody
    public String apiteacherdelete( int id){
        databaseTeacherInfo.deleteById(id);
        return "delete";
    }
    @RequestMapping("/api/deleteStudent")
    @ResponseBody
    public String apistudentdelete( int id){
        databaseStudentInfo.deleteById(id);
        return "delete";
    }*/

/*    @RequestMapping("/delete")
    public String delete(int id){
        userRepository.deleteById(id);
        return "redirect:/user/users";
    }*/

    /*@RequestMapping("/editTeacher")
    public ModelAndView editUser(int id){
        Teacher teacher = databaseTeacherInfo.getOne(id);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/teacher-edit-form.btl");
        mv.addObject("teacher",teacher);
        return  mv;

    }*/


}
